package com.wentao.thisService.service.impl;

import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.SuccessCode;
import com.wentao.thisFacade.common.enums.business.*;
import com.wentao.thisFacade.common.exception.CustomException;
import com.wentao.thisFacade.domain.PageBase;
import com.wentao.thisFacade.domain.reqeust.AddCompanyCustom;
import com.wentao.thisFacade.domain.reqeust.HotelRegisterRequest;
import com.wentao.thisFacade.domain.reqeust.OperatorListRequest;
import com.wentao.thisFacade.domain.reqeust.VerifyCompanyRequest;
import com.wentao.thisFacade.domain.result.*;
import com.wentao.thisService.common.CommonFunction;
import com.wentao.thisService.common.TokenVerify;
import com.wentao.thisService.mapper.*;
import com.wentao.thisService.model.*;
import com.wentao.thisService.model.entities.IntResultEntity;
import com.wentao.thisService.model.entities.PageEntity;
import com.wentao.thisService.service.AuthenticationService;
import com.wentao.thisService.service.CompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 10:18
 * @Description: 酒店商家service实现
 */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private AuthenticationService authenticationService;

    @Autowired
    private CommonDao commonDao;

    @Autowired
    private CompanyCustomDao companyCustomDao;

    @Autowired
    private CompanyCustomInfoDao companyCustomInfoDao;

    @Autowired
    private ApproveDao approveDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserAuthorityDao userAuthorityDao;

    @Autowired
    private PartnerDao partnerDao;

    @Autowired
    private FileDao fileDao;

    /**
     * @see CompanyService#hotelRegister(HotelRegisterRequest)
     */
    @Override
    public PhoneBackUtilsResult hotelRegister(HotelRegisterRequest hotelRegisterRequest) throws Exception {
        Timestamp nowTimestamp = commonDao.getNowTime();
        String nowTimeString = CommonFunction.GetDateByTime(nowTimestamp.getTime());

        PhoneBackUtilsResult phoneBackUtilsResult = new PhoneBackUtilsResult();

        // 校验验证码
        SmsCheckEntity smsCheckEntity = new SmsCheckEntity();
        smsCheckEntity.setMobile(hotelRegisterRequest.getMobile());
        smsCheckEntity.setCode(hotelRegisterRequest.getCode());
        boolean codeCheckFlag = authenticationService.mobileVerificationCodeCheck(smsCheckEntity.getMobile(), smsCheckEntity.getCode());

        // TODO: 2020/3/28 这一块需要改 ！！！！
        LoginCodeMsg loginCodeMsg = new LoginCodeMsg();

        if (!loginCodeMsg.getCodeMsg().equals(SuccessCode.CHECK_SUCCESS.getMessage())) {
            loginCodeMsg.setLoginUserResult(new LoginUserResult());
            phoneBackUtilsResult.setLoginCodeMsg(loginCodeMsg);
            throw new CustomException(ExceptionCode.VERIFICATION_CODE_ERROR);
        }

        String userCode = CommonFunction.CreateUUIDString();
        String passwordMD5 = CommonFunction.CreateMd5(hotelRegisterRequest.getPassword());

        // 校验是否已注册
        UserEntity searchUser = new UserEntity();
        List<UserEntity> userEntityList = userDao.Search(searchUser);
        if (null != userEntityList && userEntityList.size() >= 1) {
            phoneBackUtilsResult.setLoginMsg(CheckLoginMsgCode.USER_EXIST.getMessage());
            loginCodeMsg.setLoginUserResult(new LoginUserResult());
            phoneBackUtilsResult.setLoginCodeMsg(loginCodeMsg);
            throw new CustomException(ExceptionCode.USER_EXIST_ERROR);
        }


        // 判断邀请码是否存在
        PartnerEntity searchPartner = new PartnerEntity();
        searchPartner.setInviteCode(hotelRegisterRequest.getInviteCode());
        searchPartner.setStatus(ValidStatusEnum.VALID.getCode());
        List<PartnerEntity> partnerSearchList = partnerDao.SearchAut(searchPartner);
        if (null == partnerSearchList || partnerSearchList.size() == 0) {
            phoneBackUtilsResult.setLoginMsg(CheckLoginMsgCode.INVITATION_CODE_INEXISTENCE.getMessage());
            loginCodeMsg.setLoginUserResult(new LoginUserResult());
            phoneBackUtilsResult.setLoginCodeMsg(loginCodeMsg);
            throw new CustomException(ExceptionCode.INVITE_CODE_ERROR);
        }
        Long partnerUserId = partnerSearchList.get(0).getUserId();
        Long partnerId = partnerSearchList.get(0).getId();

        // 保存用户
        UserEntity saveUser = new UserEntity();
        saveUser.setUserName(hotelRegisterRequest.getMobile());
        saveUser.setUserCode(userCode);
        saveUser.setPassword(passwordMD5);
        saveUser.setMobile(hotelRegisterRequest.getMobile());
        saveUser.setLockStatus(UserLockStatusEnum.UN_LOCK.getCode());
        saveUser.setUserSource(partnerUserId);
        saveUser.setStatus(ValidStatusEnum.VALID.getCode());
        saveUser.setOwnId(partnerUserId);
        saveUser.setCreateBy(String.valueOf(partnerUserId));
        saveUser.setCreateTime(nowTimeString);
        userDao.Insert(saveUser);
        // TODO: 2020/3/27  createUserInfo是为了保存新建用户信息到redis
//        authorityServiceUser.createUserInfo(baseEntity);

        // 保存用户角色
        UserAuthorityEntity saveUserAuthority = new UserAuthorityEntity();
        saveUserAuthority.setUserCode(userCode);
        saveUserAuthority.setRoleCode(RoleCodeEnum.HOTEL.getCode());
        saveUserAuthority.setStatus(ValidStatusEnum.VALID.getCode());
        saveUserAuthority.setOwnId(partnerSearchList.get(0).getId());
        saveUserAuthority.setCreateBy(String.valueOf(saveUser.getId()));
        saveUserAuthority.setCreateTime(CommonFunction.GetNowDateString());
        userAuthorityDao.Insert(saveUserAuthority);

        Long userId = saveUser.getId();

        // 保存酒店信息
        String uuidCompanyCode = CommonFunction.CreateUUIDString();
        CompanyCustomEntity saveCompanyCustom = new CompanyCustomEntity();
        saveCompanyCustom.setCompanyCode(uuidCompanyCode);
        saveCompanyCustom.setUserId(userId);
        saveCompanyCustom.setPartnerId(partnerId);
        saveCompanyCustom.setRegistTime(nowTimeString);
        saveCompanyCustom.setApproveStatus(ApproveStatusEnum.IN_REVIEW.getCode());
        saveCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        saveCompanyCustom.setOwnId(userId);
        saveCompanyCustom.setCreateBy(String.valueOf(partnerUserId));
        saveCompanyCustom.setCreateTime(nowTimeString);
        companyCustomDao.Insert(saveCompanyCustom);

        phoneBackUtilsResult.setId(saveCompanyCustom.getId());
        phoneBackUtilsResult.setUserId(userId);
        phoneBackUtilsResult.setPartnerId(partnerId);
        phoneBackUtilsResult.setCompanyCode(uuidCompanyCode);
        phoneBackUtilsResult.setLoginCodeMsg(loginCodeMsg);

        return phoneBackUtilsResult;
    }

    /**
     * @see CompanyService#addCompanyCustom(AddCompanyCustom)
     */
    @Override
    @TokenVerify
    public Boolean addCompanyCustom(AddCompanyCustom addCompanyCustom) throws Exception {
        // 酒店类型由字符串转换为代码，如："商务型酒店"------>"1"
        String companyType = addCompanyCustom.getCompanyType();
        // TODO: 2020/3/27 写个方法，根据枚举code-->message互换
        String companyTypeCode;
        switch (companyType) {
            case "商务型酒店":
                companyTypeCode = "1";
                break;
            case "度假型酒店":
                companyTypeCode = "2";
                break;
            case "长住型酒店":
                companyTypeCode = "3";
                break;
            case "会议型酒店":
                companyTypeCode = "4";
                break;
            case "经济型酒店":
                companyTypeCode = "5";
                break;
            case "个性化主题酒店":
                companyTypeCode = "6";
                break;
            default:
                companyTypeCode = "0";
                break;
        }

        // 查酒店
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setCompanyCode(addCompanyCustom.getCompanyCode());
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }

        // TODO: 2020/3/27 根据传入的经纬度获取adcode等，保存
        // TODO: 2020/3/27 更新redis用户缓存的状态

        for (CompanyCustomEntity companyCustomEntity : companyCustomEntityList) {
            companyCustomEntity.setCompanyType(companyTypeCode);
            companyCustomEntity.setCompanyContact(addCompanyCustom.getCompanyContact());
            companyCustomEntity.setAddress(addCompanyCustom.getDetail());
            companyCustomEntity.setLongitude(addCompanyCustom.getLongitude());
            companyCustomEntity.setLatitude(addCompanyCustom.getLatitude());
            companyCustomEntity.setRegistrant(addCompanyCustom.getRegistrant());
            companyCustomEntity.setRegistrantPost(addCompanyCustom.getRegistrantPost());
            companyCustomEntity.setContact(addCompanyCustom.getContact());
            companyCustomEntity.setApproveStep(ApproveStepEnum.INFO_COMPLETED.getCode());

            companyCustomDao.UpdateById(companyCustomEntity);
        }

        return true;
    }

    /**
     * @see CompanyService#operatorInformationEntry(OperatorListRequest)
     */
    @Override
    @TokenVerify
    public Boolean operatorInformationEntry(OperatorListRequest operatorListRequest) throws Exception {
        Timestamp nowTimestamp = commonDao.getNowTime();
        String nowTimeString = CommonFunction.GetDateByTime(nowTimestamp.getTime());

        // 查酒店
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setCompanyCode(operatorListRequest.getCompanyCode());
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchCompanyCustom = companyCustomEntityList.get(0);

        // 查找t_companyCustomInfo酒店资质信息，有记录说明以前提交过审核被驳回，此时进行逻辑删除
        CompanyCustomInfoEntity searchCompanyCustomInfo = new CompanyCustomInfoEntity();
        searchCompanyCustomInfo.setCompanyCustomId(searchCompanyCustom.getId());
        searchCompanyCustomInfo.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomInfoEntity> companyCustomInfoEntityList = companyCustomInfoDao.Search(searchCompanyCustomInfo);
        if (null != companyCustomInfoEntityList && companyCustomInfoEntityList.size() > 0) {
            for (CompanyCustomInfoEntity companyCustomInfoEntity : companyCustomInfoEntityList) {
                companyCustomInfoEntity.setStatus(ValidStatusEnum.INVALID.getCode());
                companyCustomInfoDao.UpdateByCompanyCustomId(companyCustomInfoEntity);
            }
        }

        // 保存酒店资质
        CompanyCustomInfoEntity saveCompanyCustomInfo = new CompanyCustomInfoEntity();
        saveCompanyCustomInfo.setCompanyCustomId(searchCompanyCustom.getId());
        saveCompanyCustomInfo.setBoss(operatorListRequest.getBoss());
        saveCompanyCustomInfo.setContact(searchCompanyCustom.getContact());
        saveCompanyCustomInfo.setLicenseCode(operatorListRequest.getLicenseCode());
        saveCompanyCustomInfo.setStatus(ValidStatusEnum.VALID.getCode());
        saveCompanyCustomInfo.setOwnId(searchCompanyCustom.getUserId());
        saveCompanyCustomInfo.setCreateBy(String.valueOf(searchCompanyCustom.getId()));
        saveCompanyCustomInfo.setCreateTime(nowTimeString);
        companyCustomInfoDao.Insert(saveCompanyCustomInfo);

        // 更新酒店状态
        searchCompanyCustom.setApproveStep(ApproveStepEnum.LICENSE_COMPLETED.getCode());
        companyCustomDao.UpdateById(searchCompanyCustom);
        // TODO: 2020/3/27 更新redis用户缓存的状态

        return true;
    }

    /**
     * @see CompanyService#signContract(String)
     */
    @Override
    @TokenVerify
    public SignContractResult signContract(String companyCode) throws Exception {
        Timestamp nowTimeStamp = commonDao.getNowTime();
        String nowTimeString = CommonFunction.GetDateByTime(nowTimeStamp.getTime());

        // 查询酒店信息
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setCompanyCode(companyCode);
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchCompanyCustom = companyCustomEntityList.get(0);

        // 查询酒店资质
        CompanyCustomInfoEntity searchCompanyCustomInfo = new CompanyCustomInfoEntity();
        searchCompanyCustomInfo.setCompanyCustomId(searchCompanyCustom.getId());
        searchCompanyCustomInfo.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomInfoEntity> companyCustomInfoEntityList = companyCustomInfoDao.Search(searchCompanyCustomInfo);
        if (null == companyCustomInfoEntityList || companyCustomInfoEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchCompanyCustomInfo = companyCustomInfoEntityList.get(0);

        // 通过审核的酒店，才能签订协议
        if (!(ApproveStepEnum.LICENSE_COMPLETED.getCode().equals(searchCompanyCustom.getApproveStep())
                && (ApproveStatusEnum.REVIEW_ACCEPT.getCode().equals(searchCompanyCustom.getApproveStatus())))) {
            throw new CustomException(ExceptionCode.UNAPPROVAL_ERROR);
        }

        // 查询是否已签订协议
        ContractEntity searchContract = new ContractEntity();
        searchContract.setCompanyCustomId(searchCompanyCustom.getId());
        searchContract.setApproveStatus(searchCompanyCustom.getApproveStatus());
        searchContract.setApproveStep(searchCompanyCustom.getApproveStep());
        searchContract.setStatus(ValidStatusEnum.VALID.getCode());
        List<ContractEntity> contractEntityList = contractDao.Search(searchContract);
        if (null != contractEntityList && contractEntityList.size() > 0) {
            throw new CustomException(ExceptionCode.CONTRACT_REPEAT_ERROR);
        }

        // 保存签订协议记录
        ContractEntity saveContract = new ContractEntity();
        saveContract.setContractType(ContractTypeEnum.HOTEL_SECRET.getCode());
        saveContract.setContent("中订协议...");
        saveContract.setCompanyCustomId(searchCompanyCustom.getId());
        saveContract.setApproveStatus(searchCompanyCustom.getApproveStatus());
        saveContract.setApproveStep(searchCompanyCustom.getApproveStep());
        saveContract.setStatus(ValidStatusEnum.VALID.getCode());
        saveContract.setOwnId(searchCompanyCustom.getUserId());
        saveContract.setCreateBy(searchCompanyCustom.getCompanyName());
        saveContract.setCreateTime(nowTimeString);
        contractDao.Insert(saveContract);

        // 设置返回结果
        SignContractResult signContractResult = new SignContractResult();
        signContractResult.setBoss(searchCompanyCustomInfo.getBoss());
        signContractResult.setCompanyName(searchCompanyCustom.getCompanyName());
        signContractResult.setContent("中订协议...");

        // TODO: 2020/3/27 更新redis用户缓存的状态

        return signContractResult;
    }

    /**
     * @see CompanyService#hotelDetails(String)
     */
    @Override
    @TokenVerify
    public CompanyCustomResult hotelDetails(String companyCode) throws Exception {
        // 酒店信息
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setCompanyCode(companyCode);
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchCompanyCustom = companyCustomEntityList.get(0);

        // 如果酒店审核驳回，查原因
        if (ApproveStatusEnum.REVIEW_REFUSE.getCode().equals(searchCompanyCustom.getApproveStatus())) {
            ApproveEntity searchApprove = new ApproveEntity();
            searchApprove.setCode(String.valueOf(searchCompanyCustom.getId()));
            searchApprove.setApproveType(ApproveTypeEnum.REVIEW_HOTEL.getCode());
            searchApprove.setApproveStatus(ApproveStatusEnum.REVIEW_REFUSE.getCode());
            searchApprove.setStatus(ValidStatusEnum.VALID.getCode());
            List<ApproveEntity> approveEntityList = approveDao.Search(searchApprove);
            if (null != approveEntityList && approveEntityList.size() > 0) {
                approveEntityList.sort(Comparator.comparing(ApproveEntity::getId).reversed());
                searchApprove = approveEntityList.get(0);
                searchCompanyCustom.setRemark(HotelReviewStatusEnum.REVIEW_REFUSE.getCode());
                searchCompanyCustom.setReason(searchApprove.getReason());
            }
        }

        // 酒店类型转换
        String companyTypeCode = searchCompanyCustom.getCompanyType();
        // 酒店类型由字符串转换为代码，如："商务型酒店"------>"1"
        String companyType;
        switch (companyTypeCode) {
            case "1":
                companyType = "商务型酒店";
                break;
            case "2":
                companyType = "度假型酒店";
                break;
            case "3":
                companyType = "长住型酒店";
                break;
            case "4":
                companyType = "会议型酒店";
                break;
            case "5":
                companyType = "经济型酒店";
                break;
            case "6":
                companyType = "个性化主题酒店";
                break;
            default:
                companyType = "其他";
                break;
        }
        searchCompanyCustom.setCompanyType(companyType);

        // TODO: 2020/3/27 不推荐直接返回PO，需转为VO 数据由entity转到result

        return new CompanyCustomResult();
    }

    /**
     * @see CompanyService#searchHotelGetRegList(HotelPageRequest)
     */
    @Override
    public CompanyCustomPageResult searchHotelGetRegList(HotelPageRequest hotelPageRequest) {
        // 酒店信息
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setCompanyName(hotelPageRequest.getCompanyName());
        searchCompanyCustom.setLocation(hotelPageRequest.getLocation());
        searchCompanyCustom.setApproveStatus(hotelPageRequest.getApproveStatus());
        searchCompanyCustom.setApproveStep(hotelPageRequest.getApproveStep());
        // 合伙人信息
        if (StringUtils.isNotBlank(hotelPageRequest.getPartnerName())) {
            PartnerEntity searchPartner = new PartnerEntity();
            searchPartner.setName(hotelPageRequest.getPartnerName());
            searchPartner.setStatus(ValidStatusEnum.VALID.getCode());
            List<PartnerEntity> partnerEntityList = partnerDao.Search(searchPartner);
            List<Long> partnerIdList = new ArrayList<>();
            if (null != partnerEntityList && partnerEntityList.size() > 0) {
                for (PartnerEntity partnerEntity : partnerEntityList) {
                    if (null != partnerEntity && !partnerIdList.contains(partnerEntity.getId())) {
                        partnerIdList.add(partnerEntity.getId());
                    }
                }
            }
        }

        PageEntity pageEntity = getPageEntity(hotelPageRequest.getPageEntity());

        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.SearchPage(searchCompanyCustom, pageEntity);
        IntResultEntity count = companyCustomDao.Count(searchCompanyCustom);

        // 酒店审核列表显示审核驳回的酒店信息
        if (ApproveStatusEnum.IN_REVIEW.getCode().equals(hotelPageRequest.getApproveStatus())) {
            searchCompanyCustom.setApproveStatus(ApproveStatusEnum.REVIEW_REFUSE.getCode());
            List<CompanyCustomEntity> refusedCompanyCustomEntityList = companyCustomDao.SearchPage(searchCompanyCustom, pageEntity);
            IntResultEntity refusedCount = companyCustomDao.Count(searchCompanyCustom);

            if (null != refusedCompanyCustomEntityList && refusedCompanyCustomEntityList.size() > 0) {
                companyCustomEntityList.addAll(refusedCompanyCustomEntityList);
            }
            count.setResult(count.getResult() + refusedCount.getResult());
        }

        // approveUser 保存的id转为name
        UserEntity searchUser = new UserEntity();
        List<UserEntity> userEntityList = userDao.Search(searchUser);
        Map<String, String> userIdNameMap = new HashMap<>();
        if (null != userEntityList && userEntityList.size() > 0) {
            for (UserEntity userEntity : userEntityList) {
                if (null != userEntity) {
                    userIdNameMap.put(String.valueOf(userEntity.getId()), userEntity.getUserName());
                }
            }
        }
        PartnerEntity searchPartner = new PartnerEntity();
        List<PartnerEntity> partnerEntityList = partnerDao.Search(searchPartner);
        Map<Long, String> partnerIdNameMap = new HashMap<>();
        if (null != partnerEntityList && partnerEntityList.size() > 0) {
            for (PartnerEntity partnerEntity : partnerEntityList) {
                if (null != partnerEntity) {
                    partnerIdNameMap.put(partnerEntity.getId(), partnerEntity.getName());
                }
            }
        }

        List<CompanyCustomResult> companyCustomResultList = new ArrayList<>();
        if (null != companyCustomEntityList && companyCustomEntityList.size() > 0) {
            for (CompanyCustomEntity companyCustomEntity : companyCustomEntityList) {
                // TODO: 2020/3/27 不推荐直接返回PO，需转为VO 数据由entity转到result

            }
        }

        CompanyCustomPageResult companyCustomPageResult = new CompanyCustomPageResult();
        companyCustomPageResult.setRowCount(count.getResult());
        companyCustomPageResult.setCompanyCustomResultList(companyCustomResultList);

        return companyCustomPageResult;
    }

    /**
     * @see CompanyService#verifyCompany(VerifyCompanyRequest, Long)
     */
    @Override
    public Boolean verifyCompany(VerifyCompanyRequest verifyCompanyRequest, Long userId) {
        Timestamp nowTimestamp = commonDao.getNowTime();
        String nowTimeString = CommonFunction.GetDateByTime(nowTimestamp.getTime());

        // 查酒店信息和用户信息
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setId(verifyCompanyRequest.getId());
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchCompanyCustom = companyCustomEntityList.get(0);

        UserEntity searchUser = new UserEntity();
        searchUser.setId(searchCompanyCustom.getUserId());
        searchUser.setStatus(ValidStatusEnum.VALID.getCode());
        List<UserEntity> userEntityList = userDao.Search(searchUser);
        if (null == userEntityList || userEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }
        searchUser = userEntityList.get(0);

        // 如果是驳回，先删除图片信息
        if (ApproveStatusEnum.REVIEW_REFUSE.getCode().equals(verifyCompanyRequest.getApproveStatus())) {
            FileEntity searchFile = new FileEntity();
            searchFile.setCode(searchCompanyCustom.getCompanyCode());
            searchFile.setStatus(ValidStatusEnum.VALID.getCode());
            List<FileEntity> fileEntityList = fileDao.Search(searchFile);
            if (null != fileEntityList && fileEntityList.size() > 0) {
                for (FileEntity fileEntity : fileEntityList) {
                    fileEntity.setStatus(ValidStatusEnum.INVALID.getCode());
                    fileDao.UpdateById(fileEntity);
                }
            }
        }

        // 更新酒店信息
        searchCompanyCustom.setApproveUser(String.valueOf(userId));
        searchCompanyCustom.setApproveStep(verifyCompanyRequest.getApproveStep());
        searchCompanyCustom.setApproveStatus(verifyCompanyRequest.getApproveStatus());
        searchCompanyCustom.setRemark(verifyCompanyRequest.getRemark());
        searchCompanyCustom.setReason(verifyCompanyRequest.getReason());
        companyCustomDao.UpdateById(searchCompanyCustom);

        // 保存审核记录
        ApproveEntity saveApprove = new ApproveEntity();
        saveApprove.setCode(String.valueOf(searchCompanyCustom.getId()));
        saveApprove.setApproveType(ApproveTypeEnum.REVIEW_HOTEL.getCode());
        saveApprove.setReason(verifyCompanyRequest.getReason());
        saveApprove.setRemark(verifyCompanyRequest.getRemark());
        saveApprove.setApproveUser(userId);
        saveApprove.setApproveTime(nowTimeString);
        saveApprove.setApproveStatus(verifyCompanyRequest.getApproveStatus());
        saveApprove.setStatus(ValidStatusEnum.VALID.getCode());
        saveApprove.setOwnId(userId);
        saveApprove.setCreateTime(nowTimeString);
        saveApprove.setCreateBy(String.valueOf(userId));
        approveDao.Insert(saveApprove);

        // 通过审核，发短信通知
        if (ApproveStatusEnum.REVIEW_ACCEPT.getCode().equals(verifyCompanyRequest.getApproveStatus())) {
            Map<String, String> companyNameMap = new HashMap<>();
            companyNameMap.put("name", searchCompanyCustom.getCompanyName());
//            ApplyAcceptSMSRequest applyAcceptSMSRequest= new ApplyAcceptSMSRequest("");
//            SmsResult smsResult = smsRunner.sendSmsNoSign(user.getMobile(), "SMS_186596419",companyNameMap);
//            String smsSuccessMsg = "OK";
//            if (!smsSuccessMsg.equals(smsResult.getMessage())) {
//                // 发送失败时，不进行操作
//            }
        }

        return true;
    }

    /**
     * 获取分页entity
     *
     * @param pageBase 分页信息
     * @return 分页entity
     */
    PageEntity getPageEntity(PageBase pageBase) {
        PageEntity pageEntity = new PageEntity();
        pageEntity.setPageIndex(pageBase.getPageIndex());
        pageEntity.setPageSize(pageBase.getPageSize());
        pageEntity.setOrderString(pageBase.getOrderString());
        pageEntity.setOrderType(pageBase.getOrderType());
        return pageEntity;
    }
}
